#!/bin/csh -f

if ( "$1" == "" || "$1" == "-h" || "$1" == "--help" ) then
  echo
  echo "psrchive_install: system installation of psrchive"
  echo
  echo "usage:"
  echo "  psrchive_install VERSION"
  echo
  echo "where:"
  echo "  VERSION is a sourceforge release version number"
  echo
  exit
endif

if ( ! $?PSRCHIVE_SYSTEM ) then
  echo
  echo "psrchive_install: the PSRCHIVE_SYSTEM environment variable must be set"
  echo
  exit
endif


# path in which the psrchive distribution will be installed
#
set installation_path=$PSRCHIVE_SYSTEM/$1

if (-d $installation_path/bin) then
  echo
  echo "psrchive_install: version $1 already installed in $PSRCHIVE_SYSTEM"
  echo
  exit
endif


# make the path to which the source code will be downloaded
#

if ( ! -d $installation_path/src ) mkdir -p $installation_path/src

echo "Will install psrchive $1 in $installation_path"


# name of the sourcecode distribution file
#
set tarfile=psrchive-$1.tar.gz


if ( -f $tarfile ) then

  echo "Copying ./$tarfile ..."
  cp $tarfile $installation_path/src

endif

cd $installation_path/src

if ( ! -f $tarfile ) then

  # program to be used to download source code
  #

  if ( -x `which lynx` ) set psrchive_download = lynx
  if ( -x `which curl` ) set psrchive_download = curl
  if ( -x `which wget` ) set psrchive_download = wget

  if ( ! $?psrchive_download ) then
    echo
    echo "psrchive_install: no suitable download program (lynx curl or wget)"
    echo
    exit -1
  endif

  # the SourceForge download server will redirect to the nearest mirror
  #
  set sourceforge=http://downloads.sourceforge.net

  echo -n "Downloading $tarfile from sourceforge "

  if ( $psrchive_download == "wget" ) then
    echo "using wget ..."
    wget $sourceforge/psrchive/$tarfile
  endif

  if ( $psrchive_download == "curl" ) then
    # curl options used:
    #   -L  follow any mirror location specified by the server
    #   -f  fail silently
    #   -O  write to file named like remote file

    echo "using curl ..."
    curl --connect-timeout 30 -f -L -O $sourceforge/psrchive/$tarfile
  endif

  if ( $psrchive_download == "lynx" ) then
    echo "using lynx ..."
    lynx -source $sourceforge/psrchive/$tarfile > $tarfile
  endif

  if ( $status != 0 ) exit -1

endif

echo "Unpacking $tarfile ..."
gunzip -c $tarfile | tar xf -


echo "Configuring distribution ..."
cd psrchive-$1
./configure --prefix=$installation_path

echo "Compiling ..."
make

if ( $status != 0 ) exit -1

echo "Checking ..."
make check

if ( $status != 0 ) exit -1

echo "Installing ..."
make install

if ( $status != 0 ) exit -1

echo "Setting permissions ..."

# chmod and chgrp options used:
#   -R  change all files in all directories
#
chmod -R a-w $installation_path

if ($?PSRCHIVE_SYSGRP) then

  echo "Setting group ownership to $PSRCHIVE_SYSGRP"
  chgrp -R $PSRCHIVE_SYSGRP $installation_path

endif

echo "Linking stable to $1."
cd $PSRCHIVE_SYSTEM

#
# for those familiar with atcsver
#
rm -f current
ln -sf $1 current

#
# for us pulsar astronomers
#
rm -f stable
ln -sf $1 stable

#
# note that the "next" and "develop" symbolic links always point to the
# development area, and must be set manually
#

echo "Installation in $installation_path completed."

